#include <vector>

using namespace std;

class Solution {
private:
    int dfs(vector<vector<bool>>& visited, vector<vector<int>>& grid, int i, int j) {
        if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || visited[i][j] || grid[i][j] == 0) {
            return 0;
        }
        visited[i][j] = true;
        return 1 + dfs(visited, grid, i - 1, j) + dfs(visited, grid, i + 1, j) + dfs(visited, grid, i, j - 1) + dfs(visited, grid, i, j + 1);
    }
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        if (grid.size() == 0 || grid[0].size() == 0) {
            return 0;
        }
        int max_size = 0;
        int width = grid[0].size();
        int height = grid.size();
        vector<vector<bool>> visited(height, vector<bool>(width, false));
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                max_size = max(max_size, dfs(visited, grid, i, j));
            }
        }
        return max_size;
    }
};
